package GUI;
/**hofit**/
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

import Client.BusinessImplements;
import DAL.Member_DAL;
import DAL.SysUser_DAL;


import Client.BusinessImplements;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.border.MatteBorder;
import javax.swing.border.LineBorder;

public class Member_Card extends Abstract_Jpanel {
	private JTextField txtName;
	private JTextField txtLastName;
	private JTextField txtPhoneNumber;
	private JTextField txtStatus;
	private JTextField txtEmail;
	public JButton btnBack;
	private JPanel panel;
	private JPanel panel_1;
	private JTable table;
	private Member_Card me;
	private JTable orders_table;
	private JTable extends_table;
	/**
	 * Create the panel.
	 */
	public Member_Card() {
		me=this;
		 /**
		  * return to the main menu
		  */
		setLayout(null);
		
		JLabel lblMemmberHistory = new JLabel("Member Card");
		lblMemmberHistory.setForeground(new Color(25, 25, 112));
		lblMemmberHistory.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblMemmberHistory.setBounds(28, 0, 341, 43);
		add(lblMemmberHistory);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel_3.setBackground(SystemColor.inactiveCaption);
		panel_3.setBounds(10, 46, 888, 610);
		add(panel_3);
		panel_3.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(SystemColor.control, 6));
		panel_2.setBounds(73, 161, 755, 119);
		panel_3.add(panel_2);
		panel_2.setBackground(new Color(220, 220, 220));
		panel_2.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(25, 25, 705, 80);
		panel_2.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"Item ID", "Catalog ID", "Item Name", "Lend Date", "Return Date", "Status"
			}
		));
		table.getColumnModel().getColumn(1).setPreferredWidth(72);
		scrollPane.setViewportView(table);
		
		JLabel lblLendingBooks = new JLabel("lending items:");
		lblLendingBooks.setBounds(77, 130, 164, 20);
		panel_3.add(lblLendingBooks);
		lblLendingBooks.setFont(new Font("MV Boli", Font.BOLD, 20));
		
		panel_1 = new JPanel();
		panel_1.setBounds(69, 11, 759, 108);
		panel_3.add(panel_1);
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setLayout(null);
		
		JLabel lblStatus = new JLabel("Status:");
		lblStatus.setBounds(537, 22, 54, 14);
		lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.add(lblStatus);
		
		txtStatus = new JTextField();
		txtStatus.setEditable(false);
		txtStatus.setBounds(601, 21, 148, 20);
		panel_1.add(txtStatus);
		txtStatus.setColumns(10);
		
		JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setBounds(263, 22, 75, 14);
		lblLastName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.add(lblLastName);
		
		txtLastName = new JTextField();
		txtLastName.setEditable(false);
		txtLastName.setBounds(362, 21, 125, 20);
		panel_1.add(txtLastName);
		txtLastName.setColumns(10);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 22, 54, 14);
		lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.add(lblName);
		
		txtName = new JTextField();
		txtName.setEditable(false);
		txtName.setBounds(82, 21, 125, 20);
		panel_1.add(txtName);
		txtName.setColumns(10);
		
		JLabel lblPhoneNumber = new JLabel("Phone Number:");
		lblPhoneNumber.setBounds(10, 56, 97, 14);
		lblPhoneNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.add(lblPhoneNumber);
		
		txtPhoneNumber = new JTextField();
		txtPhoneNumber.setEditable(false);
		txtPhoneNumber.setBounds(117, 55, 148, 20);
		panel_1.add(txtPhoneNumber);
		txtPhoneNumber.setColumns(10);
		
		JLabel lblEmailAddress = new JLabel("Email Address:");
		lblEmailAddress.setBounds(307, 56, 275, 14);
		lblEmailAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.add(lblEmailAddress);
		
		txtEmail = new JTextField();
		txtEmail.setEditable(false);
		txtEmail.setBounds(419, 55, 280, 20);
		panel_1.add(txtEmail);
		txtEmail.setColumns(10);
		
		JLabel lblItemsOrders = new JLabel("items orders:");
		lblItemsOrders.setFont(new Font("MV Boli", Font.BOLD, 20));
		lblItemsOrders.setBounds(77, 291, 164, 20);
		panel_3.add(lblItemsOrders);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new LineBorder(SystemColor.control, 6));
		panel_4.setLayout(null);
		panel_4.setBackground(new Color(220, 220, 220));
		panel_4.setBounds(73, 322, 755, 119);
		panel_3.add(panel_4);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(25, 25, 705, 80);
		panel_4.add(scrollPane_1);
		
		orders_table = new JTable();
		orders_table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
			},
			new String[] {
				"item name", "date", "status", "status Date"
			}
		));
		scrollPane_1.setViewportView(orders_table);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new LineBorder(SystemColor.control, 6));
		panel_5.setLayout(null);
		panel_5.setBackground(new Color(220, 220, 220));
		panel_5.setBounds(73, 480, 755, 119);
		panel_3.add(panel_5);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(25, 25, 705, 80);
		panel_5.add(scrollPane_2);
		
		extends_table = new JTable();
		extends_table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
			},
			new String[] {
				"item Name", "Date Of Lending", "Date Of Extend", "Authorized ID"
			}
		));
		scrollPane_2.setViewportView(extends_table);
		
		JLabel lblExstendsItems = new JLabel("exstends items:");
		lblExstendsItems.setFont(new Font("MV Boli", Font.BOLD, 20));
		lblExstendsItems.setBounds(77, 449, 164, 20);
		panel_3.add(lblExstendsItems);
		 panel = new JPanel();
		 panel.setBounds(771, 655, 127, 55);
		 add(panel);
		 panel.setLayout(null);
		 
		  btnBack = new JButton("Back");
		  btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		  btnBack.setBounds(10, 11, 98, 33);
		  panel.add(btnBack);
		 btnBack.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		BusinessImplements.GetInstance().getMainFrame().displayMenu();
		 		Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();
		 		
		 		
		 	}
		 });

	}
	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @param member - the member details
	 * @param userHistory - history lends of the member
	 * the function set all the data on the screen and on the table
	 */
	public void setData(Member_DAL member, String status, ArrayList<String> userHistory, ArrayList<String> userorders, ArrayList<String> userExtends) {
		txtName.setText(member.getUserName());
		txtLastName.setText(member.getUserLastName());
		txtStatus.setText(status);
		txtEmail.setText(member.getEmail());
		txtPhoneNumber.setText(member.getPhoneNumber());
		
		if(userHistory==null||userHistory.get(0)==null)
			userHistory=new ArrayList<String>();
		String[] colums=new String[]{"Item ID", "Catalog ID", "Item Name", "Lend Date", "Return Date", "Status"};
        Object[][] data=new Object[userHistory.size()][6];
        for (int i = 0; i < userHistory.size(); i++) {
        	String[] hist= userHistory.get(i).split(",");
        	data[i][0]=hist[0];
        	data[i][1]=hist[2];
        	data[i][2]=hist[1];
        	data[i][3]=hist[3];
        	if (hist[5].equalsIgnoreCase("null"))
        		data[i][4]="";
        	else
        		data[i][4]=hist[5];
        	data[i][5]=hist[4];
        }
        table.setModel(new DefaultTableModel(data,colums));	
        	
        if(userorders==null||userorders.get(0)==null||userorders.get(0).equalsIgnoreCase("false"))
        	userorders=new ArrayList<String>();
        String[] colums1=new String[]{"item name", "date", "status", "status Date"};
        Object[][] data1=new Object[userorders.size()][4];
        for (int i = 0; i < userorders.size(); i++) {
        	String[] hist= userorders.get(i).split(",");
        	data1[i][0]=hist[0];
        	data1[i][1]=hist[1];
        	if (hist[2].equalsIgnoreCase("1"))
        		data1[i][2]="new order";
        	else
        		data1[i][2]="waiting on the library";
        	data1[i][3]=hist[3];
		}
       
        orders_table.setModel(new DefaultTableModel(data1,colums1));
        
        if(userExtends==null||userExtends.get(0)==null||userExtends.get(0).equalsIgnoreCase("false"))
        	userExtends=new ArrayList<String>();
        String[] colums2=new String[]{"item Name", "Date Of Lending", "Date Of Extend", "Authorized ID"};
        Object[][] data2=new Object[userExtends.size()][4];
        for (int i = 0; i < userExtends.size(); i++) {
        	String[] hist= userExtends.get(i).split(",");
        	data2[i][0]=hist[0];
        	data2[i][1]=hist[1];
        	data2[i][2]=hist[2];
        	data2[i][3]=hist[3];
		}
       
        extends_table.setModel(new DefaultTableModel(data2,colums2)); 
	}
}
